<?php

namespace App\model;

use Carbon\Carbon;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;

/**
 * App\City
 *
 * @property int $id
 * @property string|null $title 城市名称
 * @property int|null $parent_id
 * @property int|null $order 排序
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\City whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\City whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\City whereOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\City whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\City whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\City whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\City[] $children
 * @property-read \App\City|null $parent
 * @property int|null $is_hot 0 不是热门 1 热门
 * @method static \Illuminate\Database\Eloquent\Builder|\App\City whereIsHot($value)
 * @property-read int|null $children_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\City newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\City newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\City query()
 */
class City extends Model
{
    //


    use ModelTree,AdminBuilder;
    protected $table="city";
    public static function getSelectOptions($id=0)
    {
        $options = \DB::table('city')->select('id','title as text')->where('parent_id','=',$id)->get();
        $selectOption = [];
        foreach ($options as $option){
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }
    public static  function getList(){
       return  City::pluck('title','id')->toArray();
    }
    public function children()
    {
        return $this->hasMany(self::class,'parent_id');
    }
    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id');
    }
    public function brothers()
    {

        return $this->parent->children();
    }

    public static function option($id)
    {
        if (! $self = static::find($id)) {
            return [];
        }

        return $self->brothers()->pluck('title', 'id');
    }
}
